import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login', '/auth-redirect' , '/faceLoginPage'] // no redirect whitelist

// 路由守卫
router.beforeEach(async(to, from, next) => {
  NProgress.start()

  // 设置标题
  document.title = getPageTitle(to.meta.title)

  // 获取当前页面的token
  const hasToken = getToken()
  if (hasToken) {
    if (to.path === '/login') {
      // 如果是登录路劲，直接跳转到首页
      next({ path: '/' })
      NProgress.done()
    } else {
      // 判断用户是否拥有角色
      const hasRoles = store.getters.name !== ''
      if (hasRoles) {
        next()
      } else {
        try {
          // 获取用户消息
          await store.dispatch('user/getInfo')

          // 构造动态路由（菜单和权限）
          const accessRoutes = await store.dispatch('menu/getMenus')
          // 动态添加路由
          router.addRoutes(accessRoutes)

          next({ ...to, replace: true })
        } catch (error) {
          // 如果异常跳转到的登录页面
          await store.dispatch('user/resetToken')
          next(`/login?redirect=${to.path}`)
          NProgress.done()
        }
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
